import { Container } from 'pixi.js'
import { Resource } from "../resource.js";
import { LoginScene } from "./loginScene.js";
import { MapScene } from "./mapScene.js";


export class Scene {
  constructor(app) {
    this.nowScene = "mapScene";
    const MapConfig = {
      map: {
        mapSort: "Map0",
        mapId: "002000000.img",
      },
      player: {
        Info: {
          bodyId: "00002003.img",
          headId: "00012003.img",
          faceId: "00020400.img",
          weaponId: "01302914.img",
          hairId: "00041891.img",
          coatId: "01040141.img",
          pantsId: "01061001.img",
        },
      },
    };
    // stage
    this.app = app;
    app.stage.sortableChildren = true;
    this.stage = new Container();
    this.backStage = new Container();
    this.frontStage = new Container();
    this.stage.sortableChildren = true;

    app.stage.addChild(this.backStage);
    app.stage.addChild(this.stage);
    app.stage.addChild(this.frontStage);

    // resource
    this.resource = new Resource();

    // scene
    this.scenes = {
      mapScene: new MapScene(
        this.resource,
        this.app,
        this.stage,
        this.backStage,
        MapConfig
      ),
      loginScene: new LoginScene
    };
  }

  async init() {
    const stage = this.stage;
    const resource = this.resource;

    await resource.loadEffect()

    await this.scenes.mapScene.init();

    console.log(this.resource);
  }

  async update() {
    await this.scenes[this.nowScene].update();
  }

  async switchScene(sceneName) {
    this.scenes[this.nowScene].destroy();
    this.nowScene = sceneName;
    await this.scenes[this.nowScene].init()
  }

  // async initOperate() {
  //   const frontStage = this.frontStage;
  //   const resource = this.resource;
  //   await resource.loadOutSide("/", "./OutSide/operate.json");
  //   const operate = this.resource.Content.OutSide.operate;

  //   for (const key of Object.keys(operate)) {
  //     if (key === '$') {
  //       continue;
  //     }
  //     const {value, x, y, scale, baseX, baseY} = operate[key];
  //     const image = await loadBase64(value);
  //     const sprite = imageToSprite(image);
  //     sprite.scale.set(scale);
  //     if (baseX === 0) {
  //       sprite.x = x;
  //     } else if (baseX === 1) {
  //       sprite.x = this.app.view.width - x;
  //     }

  //     if (baseY === 0) {
  //       sprite.y = y;
  //     } else if (baseY === 1) {
  //       sprite.y = this.app.view.height - y;
  //     }

  //     frontStage.addChild(sprite);
  //     operateUI[key] = sprite;
  //   }
  //   await initOperate();
  // }
}
